﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComplexNumber
{
    class ComplexNumber
    {
        private double real;
        private double imaginary;

        public double Real
        {
            get { return real; }
            set { this.real = value; }
        }

        public double Imaginary
        {
            get { return imaginary; }
            set { this.imaginary = value; }
        }

        public override string ToString()
        {
            string result = "";
            if (this.real == 0 && this.imaginary == 0)
            {
                return "0";
            }
            if (this.real != 0)
            {
                result += this.real.ToString() + " ";
            }
            if (this.imaginary > 0)
            {
                if (this.real != 0)
                {
                    result += "+ ";
                }
            }
            if (this.imaginary < 0)
            {
                result += "- ";
            }
            if (this.imaginary > 0)
            {
                result += this.imaginary + "i";
            }
            if (this.imaginary < 0)
            {
                result += -this.imaginary + "i";
            }

            return result;
        }

        public ComplexNumber(double real, double imaginary)
        {
            this.real = real;
            this.imaginary = imaginary;
        }

        public static ComplexNumber operator +(ComplexNumber firstNumber, ComplexNumber secondNumber)
        {
            double a = firstNumber.real;
            double b = firstNumber.imaginary;
            double c = secondNumber.real;
            double d = secondNumber.imaginary;

            double resultReal = a + c;
            double resultImaginary = b + d;
            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static ComplexNumber operator -(ComplexNumber firstNumber, ComplexNumber secondNumber)
        {
            double a = firstNumber.real;
            double b = firstNumber.imaginary;
            double c = secondNumber.real;
            double d = secondNumber.imaginary;

            double resultReal = a - c;
            double resultImaginary = b - d;
            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static ComplexNumber operator *(ComplexNumber firstNumber, ComplexNumber secondNumber)
        {
            double a = firstNumber.real;
            double b = firstNumber.imaginary;
            double c = secondNumber.real;
            double d = secondNumber.imaginary;

            double resultReal = a * c - b * d;
            double resultImaginary = b * c + a * d;
            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static ComplexNumber operator /(ComplexNumber firstNumber, ComplexNumber secondNumber)
        {
            double a = firstNumber.real;
            double b = firstNumber.imaginary;
            double c = secondNumber.real;
            double d = secondNumber.imaginary;

            double resultReal = (a * c + b * d) / (c * c + d * d);
            double resultImaginary = (b * c - a * d) / (c * c + d * d);
            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static explicit operator double(ComplexNumber complexNumber)
        {
            double result = complexNumber.Real;
            return result;
        }

        public static implicit operator ComplexNumber(double value)
        {
            return new ComplexNumber(value, 0.0);
        }

        public double this[int index]
        {
            get
            {
                if (index == 0)
                {
                    return this.Real;
                }
                else if (index == 1)
                {
                    return this.Imaginary;
                }
                else
                {
                    throw new IndexOutOfRangeException("The index of a complex number can be only 0 or 1 !");
                }
            }
        }
    }
}
